#include "plotter.h"

void Plotter::updateGraph()
{
	fg.graph.update();
}
void Plotter::reshape()
{
	FlatApplication::reshape();
	adjustWindow();
}
void Plotter::adjustWindow()
{
	tbSizeX.setInt(window.width);
	tbSizeY.setInt(window.height);
	fg.size.set(window.width-40.0f,window.height-fg.bounds.y);
	container.build();
}
BOOL Plotter::create ()				
{
	FlatApplication::create();
	name=_T("Plotter");
	display.width=1024;
	display.height=768;
	InitInfo info;
	info.read();
	window.left=info.getInt(_T("left"));
	window.top=info.getInt(_T("top"));
	window.width=info.getInt(_T("width"));
	window.height=info.getInt(_T("height"));
	
	clearColor=INT_WHITE;

	fontSmall.setFace(_T("Courier New"));
	fontSmall.height=-12;
	add(&fontSmall);

	fd.addFilter(_T("plt"),_T("Plotter data file"));
	fd.addFilter(_T("dat"),_T("Matlab data file"));
	fd.addFilter();
	activeChnl=NULL;

	bOpen.text=_T("Open");
	bOpen.borderLine=1;
	bOpen.borderColor=INT_DGRAY;
	bOpen.activeLine=2;
	bOpen.font=&font;
	bOpen.fontColor=INT_BLACK;
	bOpen.margin.set(5,1);
	bOpen.bgColor=0xFFDDDDDD;
	bOpen.overColor=INT_WHITE;
	bOpen.pressColor=INT_WCYAN;
	bOpen.adjustSize=TRUE;
	bOpen.location.set(20,10);
	add(&bOpen);

	bSave.text=_T("Save");
	bSave.copy(bOpen);
	bSave.stick(DIR_RIGHT,5,0);
	add(&bSave);

	tbFile.borderLine=1;
	tbFile.borderColor=INT_DGRAY;
	tbFile.activeLine=2;
	tbFile.font=&font;
	tbFile.fontColor=INT_BLACK;
	tbFile.margin.set(2,1);
	tbFile.size.x=274;
	tbFile.stick(DIR_RIGHT,5,0);
	add(&tbFile);

	bAppend.text=_T("Append");
	bAppend.copy(bOpen);
	bAppend.checkable=TRUE;
	bAppend.stick(DIR_RIGHT,5,0,&tbFile);
	add(&bAppend);
	
	tbAppend.copy(tbFile);
	tbAppend.size.x=40;
	add(&tbAppend,DIR_RIGHT,5);

	bText.text=_T("Text");
	bText.copy(bAppend);
	add(&bText,DIR_RIGHT,5);
	
	bCapture.text=_T("Capture");
	bCapture.copy(bOpen);
	bCapture.stick(DIR_DOWN,0,10,&bOpen);
	add(&bCapture);

	tbCapture.copy(tbFile);
	tbCapture.text=_T("capture");
	tbCapture.size.x=120;
	tbCapture.stick(DIR_RIGHT,5,0,&bCapture);
	add(&tbCapture);

	chFormat.alignx=DIR_LEFT;
	chFormat.margin.set(10,1);
	chFormat.bgColor=INT_WHITE;
	chFormat.fontColor=INT_BLACK;
	chFormat.add(_T("jpg"));
	chFormat.add(_T("bmp"));
	chFormat.add(_T("tga"));
	chFormat.setSelectedIndex(0);
	chFormat.stick(DIR_RIGHT,2,0,&tbCapture);
	add(&chFormat);

	tbCompr.copy(tbFile);
	tbCompr.text=_T("100");
	tbCompr.size.x=35;
	tbCompr.stick(DIR_RIGHT,5,0,&chFormat);
	add(&tbCompr);

	bSize.text=_T("Size");
	bSize.copy(bOpen);
	bSize.stick(DIR_RIGHT,10);
	add(&bSize);
	tbSizeX.copy(tbFile);
	tbSizeX.size.x=45;
	tbSizeX.stick(DIR_RIGHT,5);
	add(&tbSizeX);
	tbSizeY.copy(tbSizeX);
	tbSizeY.stick(DIR_RIGHT,2);
	add(&tbSizeY);

	bExit.text=_T("Exit");
	bExit.copy(bOpen);
	bExit.pressColor=INT_WRED;
	bExit.stick(DIR_RIGHT,10);
	add(&bExit);

	fg.bgColor=INT_GRAY;
	fg.graph.clear();
	fg.graph.symBox=TRUE;
	fg.graph.margin=10;
	fg.graph.marginChannel.set(15,85);
	fg.graph.grid.set(3,3);
	fg.graph.font=&fontSmall;
	fg.graph.formatY.exposant=TRUE;
	fg.chnlReadLines=8;
	fg.margin.set(10,10);
	fg.font=&fontSmall;
	fg.fontColor=INT_BLACK;
	fg.scroll.setRatio(1);
	fg.stick(DIR_DOWN,0,10,&bCapture);
	add(&fg);
	
	return TRUE;
}
void Plotter::capture()
{
	Image graphImage;
	int quality,ext;
	const Rect<float> graphRect=fg.graph.getRect();
	quality=format.toInt(tbCompr.text,90);
	ext=chFormat.getSelectedIndex();
	String filename=tbCapture.text;
	screenshot(	&graphImage,
				(int)graphRect.x+1,(int)graphRect.y,
				(int)graphRect.width-1,(int)graphRect.height-1);
	switch(ext)
	{
	case 1:
		writeBitmap(filename+_T(".bmp"),&graphImage);
		break;
	case 2:
		writeTGA(filename+_T(".tga"),&graphImage);
		break;
	default:
		writeJPEG(filename+_T(".jpg"),&graphImage,quality);
		break;
	}
}
void Plotter::destroy()
{
	InitInfo info;
	info.addInt(_T("left"),window.left);
	info.addInt(_T("top"),window.top);
	info.addInt(_T("width"),window.width);
	info.addInt(_T("height"),window.height);
	info.write();
	FlatApplication::destroy();
}
BOOL Plotter::open(CSTR filename)
{
	File file;
	activeChnl=NULL;
	file.open(filename);
	fg.graph.load(
		file,
		!bAppend.checked,
		bText.checked,
		FALSE,
		tbAppend.text.length()?(CSTR)tbAppend.text:NULL);
	fg.build();
	return TRUE;
}
BOOL Plotter::save(CSTR filename)
{
	File file;
	file.open(filename,_T("wb"));
	fg.graph.save(file);
	return TRUE;
}
BOOL Plotter::listen(WORD id, WORD code, const void *info)
{
	if(id==bOpen.id)
	{
		if(code==FLAT_ACTION_DEFAULT)
		{
			CSTR filename;
			tbFile.setText(filename=fd.open());
			open(filename);
		}
		return TRUE;
	}
	else if(id==bSave.id)
	{
		if(code==FLAT_ACTION_DEFAULT)
		{
			CSTR filename;
			tbFile.setText(filename=fd.save());
			save(filename);
		}
		return TRUE;
	}
	else if(id==bCapture.id||
			id==tbCapture.id||
			id==tbCompr.id)
	{
		if(code==FLAT_ACTION_DEFAULT)
		{
			capture();
		}
		return TRUE;
	}
	else if(id==bSize.id||id==tbSizeX.id||id==tbSizeY.id)
	{
		if(code==FLAT_ACTION_DEFAULT)
		{
			resizeWindow(tbSizeX.getInt(),tbSizeY.getInt());
		}
		return TRUE;
	}
	else if(id==bExit.id)
	{
		if(code==FLAT_ACTION_DEFAULT)
		{
			terminate();
		}
		return TRUE;
	}
	return FALSE;
}


int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	Plotter plotter;
	return plotter.main(hInstance,hPrevInstance,lpCmdLine,nCmdShow);
}